package com.ruoyi.CheryEcos.service.impl;

import com.ruoyi.CheryEcos.domain.CheryEcos;
import com.ruoyi.CheryEcos.mapper.EcosMapper;
import com.ruoyi.CheryEcos.service.IEcosService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.List;
@Service
public class EcosServiceImpl implements IEcosService {
    private static final Logger log = LoggerFactory.getLogger(EcosServiceImpl.class);

    @Autowired
    private EcosMapper ecosMapper;
    @Autowired
    protected Validator validator;

    @Override
    public List<CheryEcos> selectCheryEcosList(CheryEcos cheryEcos) {
        return ecosMapper.selectCheryEcosList(cheryEcos);
    }

    @Override
    public CheryEcos selectCheryEcosById(Long id) {
        return ecosMapper.selectCheryEcosById(id);
    }

    @Override
    public int insertEcos(CheryEcos cheryEcos) {
        Double r = 1.0;
        r=(double) cheryEcos.get一次合格数()/cheryEcos.get总检数();
        BigDecimal bd = new BigDecimal(r);
        r = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        cheryEcos.set一次合格率(r);
        return ecosMapper.insertEcos(cheryEcos);
    }

    @Override
    public int updateEcos(CheryEcos cheryEcos) {
        return ecosMapper.updateEcos(cheryEcos);
    }

    @Override
    public int deleteEcosById(Long id) {
        return ecosMapper.deleteEcosById(id);
    }

    @Override
    public int deleteEcosByIds(Long[] ids) {
        return ecosMapper.deleteEcosByIds(ids);
    }

    @Override
    public List<CheryEcos> selectCheryEcosByIds(Long[] ids) {
        return ecosMapper.selectCheryEcosByIds(ids);
    }

    @Override
    public String importEcos(List<CheryEcos> ecosList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(ecosList) || ecosList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CheryEcos ecos : ecosList) {
            try {
                ecosMapper.insertEcos(ecos);
                successNum++;
                successMsg.append("<br/>" + successNum + "、数据ecos " + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据ecos " + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
